package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.BackUserDao;

import daoImpl.BackUserDaoImpl;
import pojo.BackUser;
import service.BackUserService;
import serviceImpl.BackUserServiceImpl;
import utils.SendSmsUtils;





@WebServlet("/BackUserServlet")
public class BackUserServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       

    public BackUserServlet() {
        super();
        
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String mark = request.getParameter("mark");
		switch(mark) {
		/*
		 *******************注册阶段***************************
		 */
		// 检查要注册的号码是否已存在
		case "checkPhoneNumber": 
			checkPhoneNumber(request,response);
			break;
		// 注册，将用户信息存入到数据库
		case "register":
			register(request,response);
			break;
		// 检查用户名是否已存在
		case "checkUserName":
			checkUserName(request,response);
			break;
		/*
		 *******************登入阶段***************************
		*/
		
		// 验证用户名和密码
		case "checkNamePsw":
			checkNamePsw(request,response);
			break;
		// 发送验证码
		case "getPhoneNumberCode": 
			sendVerfiyCode(request,response);
			break;
		// 核对验证码
		case "checkCode" : 
			checkCode(request,response);
			break;
		
		
		}
		
		
	}
	
	
	
	/*
	 *******************登入阶段方法实现***************************
	 */
	private void checkCode(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		// 获取用户输入的验证码
				String code = request.getParameter("code"); 
				//验证验证码是否正确 
				HttpSession session = request.getSession(); 
				// 获取会话作用域中验证码
				String cod = (String) session.getAttribute("code");
				if(code.equals(cod)) { 
					//判断验证码是否正确 
					String userName = request.getParameter("userName");
					String password = request.getParameter("psw");
					BackUserDao ud = new BackUserDaoImpl();
					BackUser user = ud.getUserByNamePsw(userName,password);
					session.setAttribute("user", user); 
					
					
					//获取项目名
					String path = request.getContextPath();
					//获取tomcat 协议+地址+端口号+ 获取项目名
					String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
						
					response.sendRedirect(basePath+"background/ordUserJs/index/index.jsp"); 
					//request.getRequestDispatcher("/htmlJs/ordUserIndex.jsp").forward(request, response); 
				}else { 
					request.setAttribute("msg", "验证码不正确"); 
					//response.sendRedirect("/resourceSharing/htmlJs/Login.jsp"); 
					request.getRequestDispatcher("htmlJs/Login.jsp").forward(request, response);
			}
				
	}

	private void checkNamePsw(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		BackUserService us = new BackUserServiceImpl();
		boolean bo = us.checkNamePsw(userName,password);
		PrintWriter writer = response.getWriter();
		writer.print(bo);
		
	}

	private void sendVerfiyCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 获取表单中输入的手机号码
		String phoneNumber = request.getParameter("userPhoneNumber"); 
		// 生成一个6位数的随机验证码
		String code = SendSmsUtils.getCode(6); 
		// 向用户输入的手机号发送验证码
		//SendSmsUtils.SendMesCode(phoneNumber, code); 
		HttpSession session = request.getSession(); 
		//将生成的短信验证码保存在会话作用域中，等待和输入的验证码进行对比 
		//session.setAttribute("code", "123"); 
		session.setAttribute("code", code); 
		response.getWriter().print(true);
		
	}
	
	
	
	/*
	 *******************注册阶段方法实现***************************
	 */
	
	// 检查用户名是否已经存在
	private void checkUserName(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String username = request.getParameter("userName");
		BackUserService us = new BackUserServiceImpl();
		boolean bo = us.checkUserName(username);
		PrintWriter writer = response.getWriter();
		writer.print(bo);
		
	}

	// 符合注册条件，则将用户信息存入数据库
	private void register(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String userName = request.getParameter("userName");
		String userPhoneNumber = request.getParameter("userPhoneNumber");
		String psw1 = request.getParameter("psw1");
		BackUser user = new BackUser(userName,psw1,0,userPhoneNumber,new Date());
		BackUserService us = new BackUserServiceImpl();
		boolean bo = us.saveUser(user);
		if(bo) {
			response.sendRedirect("/resourceSharing/htmlJs/Login.jsp");
		}else {
			request.getRequestDispatcher("/resourceSharing/htmlJs/register.jsp").forward(request, response);
		}
		
	}
	
	// 检查该手机号码是否已经存在
	private void checkPhoneNumber(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String userPhoneNumber = request.getParameter("userPhoneNumber");
		BackUserService us = new BackUserServiceImpl();
		boolean bo = us.checkPhoneNumber(userPhoneNumber);
		PrintWriter writer = response.getWriter();
		writer.print(bo);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
